﻿bingo.using('service/system/tableService');
bingo.using('service/system/userService')
bingo.module('system', function() {
	bingo.controller('users', function() {
		bingo.action('list', function($view, tableService, userService, $compile, $param, $location) {
			$view.$getNode().show();
			//$view准备好
			$view.onReady(function(){//onReadyAll所有子孙$view都准备好
				bingo.using('lib/layui/layui.js', function() {
				layui.use(['table', 'form'], function() {
					var option = {
						tableId: "tableId",
						clos: [{
								checkbox: true
							},
							{
								field: 'id',
								title: 'id',
								sort: true
							},
							{
								field: 'name',
								title: 'name'
							},
							{
								title: '操作',
								templet: function(row) {
									return '<a class="layui-btn layui-btn-xs" lay-event="info">查看</a>' +
										'<a class="layui-btn layui-btn-xs" lay-event="update">编辑</a>' +
										'<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>';
								}
							}
						],
						url: 'test.json',
						add: function() {
							layer.open({
								type: 1,
								title: "新增",
								skin: 'layui-layer-rim', //加上边框
								area: ['500px', '300px'], //宽高
								content: '<div class="layui-tab-item layui-show" id="maindialog" bg-route="" bg-name="maindialog"></div>',
								cancel: function() {
									$location.href('', "maindialog");
								},
								success: function(layero, index) {
									$compile().fromNode("#maindialog").compile();
									$param('info', {});
									$location.href('view/system/users/add', "maindialog");
								}
							});
						},
						delete: function(datas) {
							return userService.delete(datas);
						},
						actionFunc: function(table) {
							table.on('tool(tbsort)', function(obj) {
								var data = obj.data;
								var layEvent = obj.event;
								//var tr = obj.tr;
								if(layEvent === 'info') { //查看
									layer.open({
										type: 1,
										title: "信息详情",
										skin: 'layui-layer-rim', //加上边框
										area: ['500px', '300px'], //宽高
										content: '<div class="layui-tab-item layui-show" id="maindialog" bg-route="" bg-name="maindialog"></div>',
										cancel: function(index) {
											layer.close(index);
										},
										success: function(layero, index) {
											$compile().fromNode("#maindialog").compile();
											$param('info', data);
											$location.href('view/system/users/info', "maindialog");
										}
									});
								} else if(layEvent === 'delete') { //删除
									tableService.delete(data);
								} else if(layEvent === 'update') { //编辑
									layer.open({
										type: 1,
										title: "修改详情",
										skin: 'layui-layer-rim', //加上边框
										area: ['500px', '300px'], //宽高
										content: '<div class="layui-tab-item layui-show" id="maindialog" bg-route="" bg-name="maindialog"></div>',
										cancel: function() {
											$location.href('', "maindialog");
										},
										success: function(layero, index) {
											$compile().fromNode("#maindialog").compile();
											$param('info', data);
											$location.href('view/system/users/update', "maindialog");
										}
									});
									/*var rep = userService.update(data);
									if(rep.code = 0) {
										//同步更新缓存对应的值
										obj.update(data);
									} else {

									}*/
								}
							});
						},
						done: function() {
							//动态编译，针对layui table 动态添加的节点事件
							//$compile().fromNode("#content").compile();
						}
					};
					tableService.init(layui, option);
				});
			});
			});
		});

		bingo.action('info', function($view, $param) {
			$view.$getNode().show();
			$view.info = $param('info');
		});

		bingo.action('update', function($view, $param) {
			$view.$getNode().show();
			$view.info = $param('info');
		});

		bingo.action('add', function($view, $param) {
			$view.$getNode().show();
			$view.info = $param('info');
		});
	});
});